package com.wx.controller.userController;

import com.wx.pojo.WxUser;
import com.wx.service.IUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

@Controller
@RequestMapping("/user")
public class UserController {

    private final static Logger logger=LoggerFactory.getLogger(UserController.class);

    @Autowired
    private IUserService iUserService;

    /*
    登录页面
     */
    @GetMapping(value = "/login")
    private String login(Model model){
        model.addAttribute("wxUser",new WxUser());
        return "login";
    }

    /*
    登录
     */
    @PostMapping("/loginSubmit")
    private String loginSubmit(@Valid @ModelAttribute  WxUser wxUser, BindingResult result, HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession();
        if(result.hasErrors()){
            for (ObjectError error : result.getAllErrors()) {
                logger.info(error.getDefaultMessage());
            }
            return "login";
        }
        if (wxUser.getUsername().length()>0){
            System.out.println(wxUser.getUsername());
            WxUser model=  iUserService.getUserByLogin(wxUser.getUsername(),wxUser.getPassword());
            if (model!=null){
                if (model.getId()>0){
                    System.out.println(model.getId());
                    session.setAttribute("username",wxUser.getUsername());
                    session.setAttribute("userModel",model);
                }
                return "redirect:/home";
            }
        }
        return "login";
    }


    /*
    密码修改页面
     */
    @GetMapping(value = "/updatePassword")
    public String updatePassword(Model model){
        model.addAttribute("wxUser",new WxUser());
        return "updatePassword";
    }

    /*
    密码修改
     */
    @PostMapping("/updatePassword")
    private String updatePassword(@Valid @ModelAttribute  WxUser wxUser, BindingResult result, HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession();
        if(result.hasErrors()){
            for (ObjectError error : result.getAllErrors()) {
                logger.info(error.getDefaultMessage());
            }
            return "updatePassword";
        }
        if (wxUser.getUsername().length()>0){
            iUserService.updatePassword(wxUser.getUsername(),wxUser.getPassword());
        }
        return "/userView/updatePassword";
    }

    /*
    登出
     */
    @GetMapping(value = "/logout")
    public String logout(ModelMap map, HttpServletRequest request, HttpServletResponse response){
        logger.info(String.valueOf(request.getSession().getAttribute("username")));
        String logout="";
        if (request.getSession().getAttribute("username") != null){
            logout ="用户"+request.getSession().getAttribute("username")+"登出成功";
            request.getSession().removeAttribute("username");



        }else {
            logout="登出失败，请联系管理员";
        }
        map.addAttribute("logout",logout);
        return "logout";
    }


}
